#include "newfile.h"
#include "ui_newfile.h"


#include "mainwindow.h"

#include <QDateTime>
#include <QMessageBox>
#include <QSqlQuery>



NewFile::NewFile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewFile)
{
    ui->setupUi(this);
    ui->assessDate_dateEdit->setDate(QDate::currentDate());
    ui->surgeryDate_dateEdit->setDate(QDate::currentDate());
    ui->age_label->setText(QString::number((QDate::currentDate().year()) - (ui->birthday_dateEdit->date().year())));
}

NewFile::~NewFile()
{
    delete ui;
}

void NewFile::on_addInfoBtn_clicked()
{
    if(ui->name_lineEdit->text().isEmpty()  || ui->job_textEdit->document()->isEmpty())
        QMessageBox::information(this,"提示","请输入完整信息！");
    else if(ui->diagnose_textEdit->document()->isEmpty() || ui->history_textEdit->document()->isEmpty())
        QMessageBox::information(this,"提示","请填写诊断和病史");
    else
    {
        QString newId = ui->id_label->text();
        QString newName = ui->name_lineEdit->text();
        QString newGender;
        if(ui->maleRadio->isChecked()) newGender = "男";
        else newGender = "女";
        QString newGood;
        if (ui->left_goodRadio->isChecked()) newGood = "左侧";
        else if (ui->right_goodRadio->isChecked()) newGood = "右侧";
        else if (ui->both_goodRadio->isChecked()) newGood = "双侧";
        else newGood = "————";
        QString newBad;
        if (ui->left_badRadio->isChecked()) newBad = "左侧";
        else if (ui->right_badRadio->isChecked()) newBad = "右侧";
        else if (ui->both_badRadio->isChecked()) newGood = "双侧";
        else newBad = "————";
        QString newJob = ui->job_textEdit->document()->toPlainText();
        QString newCheckTimes = ui->checkTimes_spinBox->text();
        QString newBirthday = ui->birthday_dateEdit->date().toString("yyyy/MM/dd");
        QString newDamageDay = ui->damage_dateEdit->date().toString("yyyy/MM/dd");
        QString newSurgeryDay = ui->surgeryDate_dateEdit->date().toString("yyyy/MM/dd");
        QString newAsseessDay = ui->assessDate_dateEdit->date().toString("yyyy/MM/dd");
        QString newDoctor = ui->comboBox->currentText();
        QString newDiagnose = ui->diagnose_textEdit->document()->toPlainText();
        QString newHistory = ui->history_textEdit->document()->toPlainText();
        QString newAge = ui->age_label->text();
        QSqlQuery query;
        QString newPatient = QString("insert into patient values(\"%1\",\"%2\",\"%3\",\"%4\","
                                     "\"%5\",\"%6\",\"%7\",\"%8\",\"%9\",\"%10\",\"%11\",\"%12\",\"%13\",\"%14\")").
                arg(newId,newName,newGender,newGood,newBad,newJob,newCheckTimes).
                arg(newBirthday,newDamageDay,newSurgeryDay,newAsseessDay,newDoctor,newDiagnose,newHistory);
        if(query.exec(newPatient))
        {
            int i = QMessageBox::information(this,"成功","添加成功！","确定");
            if(i == QObject::tr("确定").toInt())
            {
                emit addRecord(newName,newAge,newBirthday,newAsseessDay,newCheckTimes,newId);
                this->close();
            }
        }
        else QMessageBox::information(this,"提示","添加失败","确定");
    }

}

void NewFile::on_name_lineEdit_textChanged(const QString &arg1)
{
    ui->id_label->setText(arg1.toCaseFolded() + (ui->birthday_dateEdit->date().toString("yyyyMMdd")));

}

void NewFile::on_birthday_dateEdit_dateChanged(const QDate &date)
{
    ui->age_label->setText(QString::number((QDate::currentDate().year()) - (date.year())));
    ui->id_label->setText(ui->name_lineEdit->text() + date.toString("yyyyMMdd"));
}

void NewFile::on_left_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->right_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
    }
}

void NewFile::on_both_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->left_goodRadio->setChecked(true);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
}

void NewFile::on_right_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->left_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
}

void NewFile::on_left_badRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->both_badRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
    }
    else
    {
        ui->right_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setCheckable(true);
        ui->both_goodRadio->setCheckable(false);
        ui->right_goodRadio->setCheckable(false);
    }
}

void NewFile::on_both_badRadio_clicked(bool checked)
{
    if( checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->left_badRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
    }
    else
    {
        ui->left_badRadio->setChecked(true);
        ui->right_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
    }
}

void NewFile::on_right_badRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
    }
    else
    {
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
    }
}
